//
//  UIButton+Extension.swift
//  weibo27
//
//  Created by mac on 16/4/16.
//  Copyright © 2016年 it.cast. All rights reserved.
//

import UIKit

//为button添加一个分类
extension UIButton{
    
    ///构造便利函数
    convenience init(imageName: String, backGroudImage: String?){
        self.init()
        //设置图片
        setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        setImage(UIImage(named: imageName + "_highlighted"), forState: UIControlState.Highlighted)
        
        //设置背景图片
        if let backGroudImage = backGroudImage{
            
            setBackgroundImage(UIImage(named: backGroudImage), forState: UIControlState.Normal)
            setBackgroundImage(UIImage(named: backGroudImage + "_highlighted"), forState: UIControlState.Highlighted)
        }
        
        
        //设置按钮的大小 自适应性和图片一样大
        sizeToFit()
    }
    
    
    convenience init(title: String, color:UIColor, imageName:String){
        
        self.init()
        
        setTitle(title, forState: .Normal)
        
        setTitleColor(color, forState: .Normal)
        
        sizeToFit()
        
        setBackgroundImage(UIImage(named: imageName), forState: .Normal)
    }
    
    // MARK: 设置底部按钮的遍历构造函数
    convenience init(title: String, fontSize: CGFloat, color:UIColor, imageName:String){
        
        self.init()
        
        setTitle(title, forState: UIControlState.Normal)
        
        setTitleColor(color, forState: UIControlState.Normal)
        
        setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        //自适应大小
        sizeToFit()
    }
}